package com.yufei.pfw;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.yufei.entity.Entity;
import com.yufei.utils.CommonUtil;

@Repository
public class DataRepository implements DataRepositoryI {
	@Autowired
	MongoOperations mongoOperations;
	@Autowired
	public
	MongoDbFactory mongoDbFactory;
	@Autowired
	MongoTemplate mongoTemplate=null;
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#save(java.lang.Object)
	 */
	@Override
	public void save(Object obj){
		String collectionName=obj.getClass().getSimpleName().toLowerCase();
		if(!mongoOperations.collectionExists(collectionName)){
			mongoOperations.createCollection(collectionName);
		}
		mongoOperations.save(obj);
		
		}
	
	//批量插入新数据，确保数据没有被保存过
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#insert(java.util.Collection)
	 */
	@Override
	public void insert(Collection objs){
		if(CommonUtil.isEmptyOrNull(objs)){
			throw new IllegalArgumentException();
		}
		Iterator<Object> iterator = objs.iterator();
		Object obj=iterator.next();
	
		String collectionName=obj.getClass().getSimpleName().toLowerCase();
		if(!mongoOperations.collectionExists(collectionName)){
			mongoOperations.createCollection(collectionName);
		}
		mongoOperations.insertAll(objs);
		}
	//save the entity into a customer collectionName
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#save(java.lang.Object, java.lang.String)
	 */
	@Override
	public void save(Object obj,String collectionName){
		
		if(!mongoOperations.collectionExists(collectionName)){
			mongoOperations.createCollection(collectionName);
		}
		mongoOperations.save(obj);
	}
	//query a entity form the collection whose collectionname is the entity's class name
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#queryEntity(java.lang.String, java.lang.Object, java.lang.Class)
	 */
	@Override
	public  <T> T queryEntity(String queryParameter,Object queryValue,Class<T> cla){
		List<T> list= mongoOperations.find(query(where(queryParameter).is(queryValue)), cla);
		if(CommonUtil.isEmptyOrNull(list)){
			return null;
		}
		return 		(T)list.get(0);
	}
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#queryListEntity(java.lang.String, java.lang.Object, java.lang.Class)
	 */
	@Override
	public <T> List<T> queryListEntity(String queryParameter,Object queryValue,Class<T> cla){
		List<T> entitys=null;
		entitys=(List<T>) mongoOperations.find(query(where(queryParameter).is(queryValue)), cla);
		return entitys;
	}
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#queryBySign(java.lang.String, java.lang.Class)
	 */
	@Override
	public <T> List<T> queryBySign(String sign,Class<T> entity){
		return mongoOperations.find(query(where("sign").is(sign)), entity);
		

		
	}
   /* (non-Javadoc)
 * @see com.yufei.pfw.DataRepositoryI#updateEntity(java.lang.String, java.lang.Class, java.lang.String, java.lang.Object)
 */
@Override
public void updateEntity(Long id,Class entity,String property,Object value){
	   
	   mongoOperations.updateFirst(query(where("_id").is(id)), new Update().update(property, value), entity);
	   
   }
   
   //查询所有特定集合中的某个字段
   /* (non-Javadoc)
 * @see com.yufei.pfw.DataRepositoryI#queryAllValuesOfField(java.lang.String, java.lang.Class, java.util.Map)
 */
@Override
public List queryAllValuesOfField(String fieldName,Class entity,Map queryMap){
          List result=new ArrayList();
          BasicDBObject fields=new BasicDBObject();
	    fields.put(fieldName, 1);
	    DBCursor dbCursor=mongoOperations.getCollection(CommonUtil.downFirstChar(entity.getSimpleName()))
	    .find(new BasicDBObject(queryMap),fields);
	    while(dbCursor.hasNext()){
	    	result.add(dbCursor.next().get(fieldName));
	    }
	  dbCursor.close();
return result;	   
   }
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#getMongoOperations()
	 */
	@Override
	public MongoOperations getMongoOperations(){
		return mongoOperations;
	}

	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#setMongoOperations(org.springframework.data.mongodb.core.MongoOperations)
	 */
	@Override
	public void setMongoOperations(MongoOperations mongoOperations) {
		this.mongoOperations = mongoOperations;
	}
	//save file
	//public void 
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#getMongoDbFactory()
	 */
	@Override
	public MongoDbFactory getMongoDbFactory() {
		return mongoDbFactory;
	}
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#setMongoDbFactory(org.springframework.data.mongodb.MongoDbFactory)
	 */
	@Override
	public void setMongoDbFactory(MongoDbFactory mongoDbFactory) {
		this.mongoDbFactory = mongoDbFactory;
	}
	/* (non-Javadoc)
	 * @see com.yufei.pfw.DataRepositoryI#removeEntity(com.yufei.entity.Entity)
	 */
	@Override
	public void removeEntity(Entity entity){
		Long _id = entity.getId();

		
		DBObject dbobject=new BasicDBObject();
		dbobject.put("_id", _id);
		String downFirstChar = CommonUtil.downFirstChar(entity.getClass().getSimpleName());
		this.mongoDbFactory.getDb().getCollection(downFirstChar).remove(dbobject);
		
		
	}

	@Override
	public void batchInsert(List<? extends Entity> entities) {
		// TODO Auto-generated method stub
		this.mongoOperations.insertAll(entities);
	}
	

}
